package storage;
import java.util.Map;
import java.util.Random;
import java.util.HashMap;
import configuration.Configuration;
/**
* The architecture of the disk that hosts the file blocks
* @author Nodirjon Siddikov
*/
public class Disk
{
//instance variables
private Map<Integer, Block> _content; //disk content
private Random _prng;
private int _noOfBlocks;
private int _blockSize;
//constructor
public Disk(int noOfBlocks) throws Exception
{
_prng = new Random(new Random().nextLong());
_content = new HashMap<Integer, Block>();
_blockSize = Configuration.blockSize;
_noOfBlocks = noOfBlocks;
}
//populate the disk with blocks
public void populate() throws Exception
{
for(int i = 0; i < _noOfBlocks; i++)
{
//generate the block ID
int blockID = _prng.nextInt(Configuration.noOfBlocks);
//check for duplicate block IDs
while(_content.containsKey(blockID))
{
blockID = _prng.nextInt(Configuration.noOfBlocks);
}
//create a block
Block b = new Block(blockID, _blockSize);
//add the block to the disk
_content.put(blockID, b);
}
}
//read the block
public Block read(int blockID)
{
//returns null if disk has no block with such ID
return _content.get(blockID);
}
//get the size of the disk
public int getSize()
{
return _noOfBlocks * _blockSize;
}
}